<%@include file="/ui/jsp/includes/includes.jspf" %> 
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<script type="text/javascript">
$(document).ready(function () {
	$("#nNumberMatch").hide();
   
});
function addNewFaculty()
{
	$('#addNewFacultyForm').attr('action', 'addNewFaculty.html');
	$('#addNewFacultyForm').submit();
}
function updateFaculty()
{
	$('#facultyId').val('${faculty.facultyId}');
	$('#addNewFacultyForm').attr('action', 'updateFaculty.html');
	$('#addNewFacultyForm').submit();
}
function backToFacultyList()
{
	$('#addNewFacultyForm').attr('action', 'viewFacultyList.html');
	$('#addNewFacultyForm').submit();
}
function checkNNumber(){
	var nNumberMap = '${nNumberMap}';
	var nNumberStr = nNumberMap.substring(nNumberMap.indexOf('{') + 1, nNumberMap.indexOf('}'));
    nNumberStr = nNumberMap.replace(/=/g, ":");
    var nNumberArr = nNumberStr.split(',');
    var tempArray = new Array();
    var tempStr = "";
    for (var i = 0; i < nNumberArr.length; i++) {
        tempStr = nNumberArr[i];
        tempStr = tempStr.substring(tempStr.indexOf(":") + 1);
        tempArray[i] = tempStr;
    }
	var nnumber = $("#nnumber").val();
	var match = false;
	
	//alert(tempArray);
	if(nnumber != ""){
    	for (var j = 0; j < tempArray.length; j++){
    		var studentNumber = tempArray[j];
    		if(nnumber === studentNumber){
    			match = true;
    			//alert(studentNumber);
    		}
    	}
	}
	//alert(match);
	if(match == true){
		$("#nNumberMatch").html("This N Number has already been used.");
		$("#nNumberMatch").show();
	}
	else{
		$("#nNumberMatch").html("");
		$("#nNumberMatch").hide();
	}
}

</script>
<%@include file="/ui/jsp/includes/header.jspf"%>
<div class="hero-unit secondary_container">
    <div id="table-subject-header">
        <c:if test="${!empty faculty.nnumber}">
        	Update Faculty
        </c:if>
        <c:if test="${empty faculty.nnumber}">
        	Add Faculty
        </c:if>
    </div>
        <hr>

    <div class="row-fluid">
        <form:form id="addNewFacultyForm" method="POST" commandName="faculty"  class="form-horizontal">
            <form:errors path="*" cssClass="errorblock" element="div"/>
            <div class="errorblock" id="nNumberMatch"></div>
            <div class="control-group">
                <label class="control-label" for="nnumber">N Number* </label>
                <div class="controls">
                    <form:input id="nnumber" path="nnumber" onchange="checkNNumber()"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="firstName">First Name* </label>
                <div class="controls">
                    <form:input id="firstName" path="firstName"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="lastName">Last Name* </label>
                <div class="controls">
                    <form:input id="lastName" path="lastName"/>
                </div>
            </div>
                <div class="control-group">
                    <label class="control-label" for="email">Email* </label>
                    <div class="controls">
                        <form:input id="email" path="email"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="webpageUrl">Web Page URL</label>
                    <div class="controls">
                        <form:input id="webpageUrl" path="webpageUrl"/>
                    </div>
                </div>
                <div class="control-group">               
                    <label class="control-label">Active?</label>
                    <div class="controls">
                        <form:radiobutton path="inactive" value="N" /> Yes
                        <form:radiobutton path="inactive" value="Y" /> No
                    </div>
                </div>
                <div>
                	*Required Fields
                </div>
                <div class="control-group">
                    <div class="controls">
                    <c:choose>
                    <c:when test="${!empty faculty.facultyId}">
                     <form:hidden path="facultyId" id="facultyId"/>
                    <input type="button" onclick="javascript:updateFaculty()" value="Submit" class="btn btn-primary"/>
                    </c:when>
                    <c:otherwise>
                    	<input type="button" onclick="javascript:addNewFaculty()" value="Submit" class="btn btn-primary"/>
                    </c:otherwise>
                    </c:choose>
                    <input type="button" value="Cancel" onclick="javascript:backToFacultyList()" class="btn btn-primary"/>
                    </div>
                </div>
               
                </form:form>	
        </div>
    </div>

    <%@include file="/ui/jsp/includes/footer.jspf"%>